package com.shsc.example.project;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.location.Address;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Intent mServiceIntent;
    private MusicService.MyBinder mMusicControl;
    private MyConnection mConn;
    public static final String RECEIVE_FLAG = "com.shuhai.broadcastreceiver.MYRECEIVER";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btView = (Button) findViewById(R.id.bt_view);
        Button btLifeCycle = (Button) findViewById(R.id.bt_life_cycle);
        Button btPlayMusic = (Button) findViewById(R.id.bt_play_music);
        Button btStopMusic = (Button) findViewById(R.id.bt_stop_music);
        Button btReceiver = (Button) findViewById(R.id.bt_receiver);
        Button btThread = (Button) findViewById(R.id.bt_thread);
        TextView textView = (TextView) findViewById(R.id.tv_receive);
        TextView btAddress = (TextView) findViewById(R.id.bt_address);

        btView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  跳转布局
                startActivity(new Intent(MainActivity.this, ViewActivity.class));
            }
        });

        btLifeCycle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  跳转生命周期的
                startActivity(new Intent(MainActivity.this, LifeCycleActivity.class));
            }
        });

        btPlayMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mServiceIntent = new Intent(MainActivity.this, MusicService.class);
                startService(mServiceIntent);

//                mConn = new MyConnection();
//                bindService(mServiceIntent, mConn, BIND_AUTO_CREATE);
            }
        });

        btStopMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(mServiceIntent);

//                if (null != mMusicControl) {
//                    // 停止播放
//                    mMusicControl.play();
//                    // 断开连接
//                    unbindService(mConn);
//                }
            }
        });

        btReceiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //注册广播接收器
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(RECEIVE_FLAG);
                registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        Log.e("Receiver", "收到广播action  = " + action);
                        if (action.equals(RECEIVE_FLAG)) {
                            // 接收到广播传来的数据
                            String name = intent.getStringExtra("name");
                            Log.e("Receiver", "解析数据 Receiver get name: " + name);

                            // 更新页面数据
                            textView.setText("接收到广播数据，变更UI，" + name);
                        }
                    }
                }, intentFilter);

                startActivity(new Intent(MainActivity.this, SendBrodcastRecevieAct.class));
            }
        });


        btAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AddressActivity.class));
            }
        });

        btThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ThreadActivity.class));
            }
        });
    }

    private class MyConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 连接的时候
            // 获得service中的MyBinder
            mMusicControl = (MusicService.MyBinder) service;
            Log.e("Service", "已绑定， 连接");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("Service", "断开连接");
            // 断连的时候
        }
    }
}
